import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:bruno/bruno.dart';

class CustomTopInputDialog {
  /// 显示自定义顶部输入弹窗，带滑入动画和外部点击关闭功能
  static void show(
      BuildContext context, {
        required Function(String) onConfirm,
        String title = "标题",
        String hintText = "",
        String message = "输入提示信息",
        String confirmText = "确定",
        String cancelText = "取消",
        double titleFontSize = 15,
        Color titleColor = Colors.black87,
        double messageFontSize = 12,
        Color messageColor = Colors.grey,
        double buttonFontSize = 16,
        int maxLength = 20,
        // 新增参数：控制是否允许输入任意符号
        bool isFu = false,
        // 按钮样式自定义参数
        Color cancelBgColor =  const Color(0xF5F7FBFF),
        Color cancelTextColor = Colors.black87,
        Color confirmBgColor = Colors.blueAccent,
        Color confirmTextColor = Colors.white,
        // 弹窗顶部距离参数
        double topDistance = 200,
        // 动画持续时间
        Duration animationDuration = const Duration(milliseconds: 300),
        // 提示文字颜色（默认淡灰色）
        Color hintTextColor = const Color(0xFF9E9E9E),
        // 输入框样式参数
        Color inputBorderColor = Colors.white,      // 输入框边框颜色
        Color inputBgColor = const Color(0xF5F7FBFF), // 输入框背景颜色
        double inputBorderRadius = 10.0,           // 输入框圆角半径
      }) {
    final TextEditingController _controller = TextEditingController(text: hintText);

    showGeneralDialog(
      context: context,
      barrierDismissible: true, // 点击外部可关闭
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      transitionDuration: animationDuration,
      pageBuilder: (context, animation1, animation2) {
        return const SizedBox.shrink(); // 主内容在transitionBuilder中构建
      },
      transitionBuilder: (context, animation, secondaryAnimation, child) {
        // 动画曲线：先快后慢
        final curvedAnimation = CurvedAnimation(
          parent: animation,
          curve: Curves.easeOut,
        );

        // 从上方滑入的动画
        return FadeTransition(
          opacity: curvedAnimation,
          child: Dialog(
            backgroundColor: Colors.transparent,
            elevation: 0,
            insetPadding: EdgeInsets.zero,
            child: Stack(
              children: [
                // 点击外部关闭的透明区域
                Positioned.fill(
                  child: GestureDetector(
                    onTap: () => Navigator.pop(context),
                    child: Container(color: Colors.transparent),
                  ),
                ),
                // 弹窗内容，固定在顶部位置
                Positioned(
                  top: topDistance,
                  left: 16,
                  right: 16,
                  child: Container(
                    width: double.infinity,
                    padding: const EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black12,
                          blurRadius: 10,
                          spreadRadius: 2,
                        )
                      ],
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        // 标题居中
                        Align(
                          alignment: Alignment.center,
                          child: Text(
                            title,
                            style: TextStyle(
                              fontSize: titleFontSize,
                              fontWeight: FontWeight.bold,
                              color: titleColor,
                            ),
                          ),
                        ),
                        const SizedBox(height: 12),

                        // 带清空按钮的输入框 - 已修改为灰色边框、淡灰色背景和圆角
                        TextField(
                          controller: _controller,
                          autofocus: true,
                          maxLength: maxLength,
                          decoration: InputDecoration(
                            hintText: hintText,
                            hintStyle: TextStyle(color: hintTextColor),
                            // 设置输入框背景色
                            fillColor: inputBgColor,
                            filled: true,
                            // 设置边框样式
                            border: OutlineInputBorder(
                              borderSide: BorderSide(color: inputBorderColor),
                              borderRadius: BorderRadius.circular(inputBorderRadius),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: inputBorderColor),
                              borderRadius: BorderRadius.circular(inputBorderRadius),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: inputBorderColor),
                              borderRadius: BorderRadius.circular(inputBorderRadius),
                            ),
                            counterText: '',
                            // 添加清空按钮
                            suffixIcon: IconButton(
                              icon: const Icon(Icons.clear, size: 18),
                              onPressed: () => _controller.clear(),
                            ),
                          ),
                          inputFormatters: [
                            LengthLimitingTextInputFormatter(maxLength),
                            // 根据isFu参数决定是否添加字符过滤
                            if (!isFu)
                              FilteringTextInputFormatter(RegExp(r'[a-zA-Z0-9\u4e00-\u9fa5_]'), allow: true),
                          ],
                        ),
                        const SizedBox(height: 20),

                        // 提示信息
                        Text(
                          message,
                          style: TextStyle(
                            fontSize: messageFontSize,
                            color: messageColor,
                          ),
                        ),
                        const SizedBox(height: 16),

                        // 按钮区域 - 椭圆形按钮样式
                        Row(
                          children: [
                            // 取消按钮（占一半宽度）
                            Expanded(
                              child: Padding(
                                padding: const EdgeInsets.symmetric(horizontal: 8),
                                child: TextButton(
                                  style: TextButton.styleFrom(
                                    backgroundColor: cancelBgColor,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(20),
                                    ),
                                    padding: const EdgeInsets.symmetric(vertical: 8),
                                  ),
                                  onPressed: () => Navigator.pop(context),
                                  child: Text(
                                    cancelText,
                                    style: TextStyle(
                                      fontSize: buttonFontSize,
                                      color: cancelTextColor,
                                    ),
                                  ),
                                ),
                              ),
                            ),

                            // 确定按钮（占一半宽度）
                            Expanded(
                              child: Padding(
                                padding: const EdgeInsets.symmetric(horizontal: 8),
                                child: TextButton(
                                  style: TextButton.styleFrom(
                                    backgroundColor: confirmBgColor,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(20),
                                    ),
                                    padding: const EdgeInsets.symmetric(vertical: 8),
                                  ),
                                  onPressed: () {
                                    final inputText = _controller.text.trim();
                                    if (inputText.isEmpty) {
                                      BrnToast.show("请输入内容", context);
                                      return;
                                    }
                                    onConfirm(inputText);
                                    Navigator.pop(context);
                                  },
                                  child: Text(
                                    confirmText,
                                    style: TextStyle(
                                      fontSize: buttonFontSize,
                                      color: confirmTextColor,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
